home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / orca / find.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  7.4 KB  |  212 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Provides support for a flat review find.'''
  5. __id__ = '$Id: find.py 3882 2008-05-07 18:22:10Z richb $'
  6. __version__ = '$Revision: 3882 $'
  7. __date__ = '$Date: 2008-05-07 14:22:10 -0400 (Wed, 07 May 2008) $'
  8. __copyright__ = 'Copyright (c) 2005-2008 Sun Microsystems Inc.'
  9. __license__ = 'LGPL'
  10. import copy
  11. import re
  12. import debug
  13. import flat_review
  14. import orca_state
  15. import speech
  16. from orca_i18n import _
  17.  
  18. class SearchQuery:
  19.     '''Represents a search that the user wants to perform.'''
  20.     
  21.     def __init__(self):
  22.         '''Creates a new SearchQuery. A searchQuery has the following
  23.            properties:
  24.  
  25.            searchString     - the string to find
  26.            searchBackwards  - if true, search upward for matches
  27.            caseSensitive    - if true, case counts
  28.            matchEntireWord  - if true, only match on the entire string
  29.            startAtTop       - if true, begin the search from the top of
  30.                               the window, rather than at the current
  31.                               location
  32.            windowWrap       - if true, when the top/bottom edge of the
  33.                               window is reached wrap to the bottom/top
  34.                               and continue searching
  35.         '''
  36.         self.searchString = ''
  37.         self.searchBackwards = False
  38.         self.caseSensitive = False
  39.         self.matchEntireWord = False
  40.         self.windowWrap = False
  41.         self.startAtTop = False
  42.         self.debugLevel = debug.LEVEL_FINEST
  43.  
  44.     
  45.     def debugContext(self, context, string):
  46.         '''Prints out the context and the string to find to debug.out'''
  47.         debug.println(self.debugLevel, '------------------------------------------------------------')
  48.         debug.println(self.debugLevel, 'findQuery: %s line=%d zone=%d word=%d char=%d' % (string, context.lineIndex, context.zoneIndex, context.wordIndex, context.charIndex))
  49.         debug.println(self.debugLevel, 'Number of lines: %d' % len(context.lines))
  50.         debug.println(self.debugLevel, 'Number of zones in current line: %d' % len(context.lines[context.lineIndex].zones))
  51.         debug.println(self.debugLevel, 'Number of words in current zone: %d' % len(context.lines[context.lineIndex].zones[context.zoneIndex].words))
  52.         debug.println(self.debugLevel, '==========================================================\n\n')
  53.  
  54.     
  55.     def dumpContext(self, context):
  56.         '''Debug utility which prints out the context.'''
  57.         print 'DUMP'
  58.         for i in range(0, len(context.lines)):
  59.             print '  Line %d' % i
  60.             for j in range(0, len(context.lines[i].zones)):
  61.                 print '    Zone: %d' % j
  62.                 for k in range(0, len(context.lines[i].zones[j].words)):
  63.                     print '      Word %d = `%s` len(word): %d' % (k, context.lines[i].zones[j].words[k].string, len(context.lines[i].zones[j].words[k].string))
  64.                 
  65.             
  66.         
  67.  
  68.     
  69.     def findQuery(self, context, justEnteredFlatReview):
  70.         '''Performs a search on the string specified in searchQuery.
  71.  
  72.            Arguments:
  73.            - context: The context from active script
  74.            - justEnteredFlatReview: If true, we began the search in focus
  75.              tracking mode.
  76.  
  77.            Returns:
  78.            - The context of the match, if found
  79.         '''
  80.         originalLineIndex = context.lineIndex
  81.         originalZoneIndex = context.zoneIndex
  82.         originalWordIndex = context.wordIndex
  83.         originalCharIndex = context.charIndex
  84.         debug.println(self.debugLevel, 'findQuery: original context line=%d zone=%d word=%d char=%d' % (originalLineIndex, originalZoneIndex, originalWordIndex, originalCharIndex))
  85.         flags = re.LOCALE
  86.         if not self.caseSensitive:
  87.             flags = flags | re.IGNORECASE
  88.         
  89.         if self.matchEntireWord:
  90.             regexp = '\\b' + self.searchString + '\\b'
  91.         else:
  92.             regexp = self.searchString
  93.         pattern = re.compile(regexp, flags)
  94.         debug.println(self.debugLevel, 'findQuery: startAtTop: %d  regexp: `%s`' % (self.startAtTop, regexp))
  95.         if self.startAtTop:
  96.             context.goBegin(flat_review.Context.WINDOW)
  97.             self.debugContext(context, 'go begin')
  98.         
  99.         location = None
  100.         found = False
  101.         wrappedYet = False
  102.         doneWithLine = False
  103.         while not found:
  104.             (currentLine, x, y, width, height) = context.getCurrent(flat_review.Context.LINE)
  105.             debug.println(self.debugLevel, 'findQuery: current line=`%s` x=%d y=%d width=%d height=%d' % (currentLine, x, y, width, height))
  106.             if re.search(pattern, currentLine) and not doneWithLine:
  107.                 while not found:
  108.                     (currentZone, x, y, width, height) = context.getCurrent(flat_review.Context.ZONE)
  109.                     debug.println(self.debugLevel, 'findQuery: current zone=`%s` x=%d y=%d ' % (currentZone, x, y))
  110.                     debug.println(self.debugLevel, 'width=%d height=%d' % (width, height))
  111.                     if re.search(pattern, currentZone):
  112.                         theZone = context.lines[context.lineIndex].zones[context.zoneIndex]
  113.                         if originalLineIndex == context.lineIndex:
  114.                             pass
  115.                         startedInThisZone = originalZoneIndex == context.zoneIndex
  116.                         
  117.                         try:
  118.                             theZone.accessible.queryText()
  119.                         except:
  120.                             pass
  121.  
  122.                         allMatches = re.finditer(pattern, currentZone)
  123.                         offsets = []
  124.                         for m in allMatches:
  125.                             offsets.append(m.start(0))
  126.                         
  127.                         if self.searchBackwards:
  128.                             offsets.reverse()
  129.                         
  130.                         i = 0
  131.                         while not found and i < len(offsets):
  132.                             (nextInstance, offset) = theZone.getWordAtOffset(offsets[i])
  133.                             if nextInstance:
  134.                                 offsetDiff = nextInstance.index - context.wordIndex
  135.                                 if (self.searchBackwards or offsetDiff < 0 or not (self.searchBackwards)) and offsetDiff > 0:
  136.                                     context.wordIndex = nextInstance.index
  137.                                     context.charIndex = 0
  138.                                     found = True
  139.                                 elif not offsetDiff:
  140.                                     pass
  141.                                 None if not startedInThisZone or justEnteredFlatReview else justEnteredFlatReview
  142.                                 i += 1
  143.                                 continue
  144.                             break
  145.                     
  146.                     if not found:
  147.                         if self.searchBackwards:
  148.                             moved = context.goPrevious(flat_review.Context.ZONE, flat_review.Context.WRAP_LINE)
  149.                             self.debugContext(context, '[1] go previous')
  150.                             context.goEnd(flat_review.Context.ZONE)
  151.                             self.debugContext(context, '[1] go end')
  152.                         else:
  153.                             moved = context.goNext(flat_review.Context.ZONE, flat_review.Context.WRAP_LINE)
  154.                             self.debugContext(context, '[1] go next')
  155.                         if not moved:
  156.                             doneWithLine = True
  157.                             break
  158.                         
  159.                     moved
  160.                 continue
  161.             if self.searchBackwards:
  162.                 moved = context.goPrevious(flat_review.Context.LINE, flat_review.Context.WRAP_LINE)
  163.                 self.debugContext(context, '[2] go previous')
  164.             else:
  165.                 moved = context.goNext(flat_review.Context.LINE, flat_review.Context.WRAP_LINE)
  166.                 self.debugContext(context, '[2] go next')
  167.             if moved:
  168.                 if self.searchBackwards:
  169.                     moved = context.goEnd(flat_review.Context.LINE)
  170.                     self.debugContext(context, '[2] go end')
  171.                 
  172.             self.searchBackwards
  173.             if self.windowWrap and not wrappedYet:
  174.                 doneWithLine = False
  175.                 wrappedYet = True
  176.                 if self.searchBackwards:
  177.                     speech.speak(_('Wrapping to Bottom'))
  178.                     moved = context.goPrevious(flat_review.Context.LINE, flat_review.Context.WRAP_ALL)
  179.                     self.debugContext(context, '[3] go previous')
  180.                 else:
  181.                     speech.speak(_('Wrapping to Top'))
  182.                     moved = context.goNext(flat_review.Context.LINE, flat_review.Context.WRAP_ALL)
  183.                     self.debugContext(context, '[3] go next')
  184.                 if not moved:
  185.                     debug.println(self.debugLevel, 'findQuery: cannot wrap')
  186.                     break
  187.                 
  188.             moved
  189.             break
  190.         if found:
  191.             location = copy.copy(context)
  192.         
  193.         self.debugContext(context, 'before setting original')
  194.         context.setCurrent(originalLineIndex, originalZoneIndex, originalWordIndex, originalCharIndex)
  195.         self.debugContext(context, 'after setting original')
  196.         if location:
  197.             debug.println(self.debugLevel, 'findQuery: returning line=%d zone=%d word=%d char=%d' % (location.lineIndex, location.zoneIndex, location.wordIndex, location.charIndex))
  198.         
  199.         return location
  200.  
  201.  
  202.  
  203. def getLastQuery():
  204.     '''Grabs the last search query performed from orca_state.
  205.  
  206.        Returns:
  207.        - A copy of the last search query, if it exists
  208.     '''
  209.     lastQuery = copy.copy(orca_state.searchQuery)
  210.     return lastQuery
  211.  
  212.